/**----------------------------------------
 * @description Toast层管理
 * @author      lxc
 * @file        ToastManager.ts
 * @Date        2022-05-05
 * @version     1.0.0
 * ----------------------------------------
 */

import { MsgComponent } from "../message/MsgComponent";
import { DialogPrefab } from "../resource/PathConstant";

export class ToastLayer extends MsgComponent{

    protected logPrefix: string = "ToastManager";

    toastNode : cc.Node = null;

    toastLabel : cc.Label = null;
    // toast 存在时长
    showTime = 2000;  
    // toast 显示状态
    showState = false;

    onLoad(): void {
        // gVar.ResMgr.loadPrefab(DialogPrefab.toast, (prefab:cc.Prefab) => {
        //     this.toastNode = cc.instantiate(prefab);
        //     this.toastNode.parent = this.node;
        //     this.toastLabel = this.toastNode.getChildByName("label").getComponent(cc.Label)
        //     this.toastNode.active = false;
        // })
    }

    initListeners(): void {
        super.initListeners()
        this.addUIListener(LocalMsg.ShowToast, this.showToast.bind(this))
    }
    /**
     * 显示 toast 内容
     * @param toastContent
     */
    showToast(toastContent : string){
        this.printLog("showToast",toastContent);
        this.showState = true;
        this.toastNode.width = this.toastLabel.node.width;
        this.toastNode.height = this.toastLabel.node.height;
        this.toastLabel.string = toastContent;
        this.showTime = 2000;
        this.toastNode.active = true;
    }

    protected update(dt: number): void {
         if(this.showState){
            this.showTime -= dt * 1000;
            if (this.showTime <= 0){
                this.showState = false;
                this.toastNode.active = false;
            }
         }
    }
}